package com.lovvyang.servlet;

import com.lovvyang.dao.UserDao;
import com.lovvyang.dao.impl.UserDaoImpl;
import com.lovvyang.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lovvyang
 * @date 2020/6/8-22:25
 */
/*
 * @PackageName:com.lovvyang.servlet
 * @ClassName:LoginServlet
 * @Description
 * @author lovvyang
 * @date 2020/6/8/22:25
 **/
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserDao userDao =new UserDaoImpl();
        //设置请求编码格式
        req.setCharacterEncoding("utf-8");
        //查询用户名和密码
        String username = req.getParameter("username");

        String password = req.getParameter("password");
        //将用户名和密码封装为User对象
        User user =new User();
        user.setUsername(username);
        user.setPassword(password);
        //调用dao层的UserDao 判断是否正确
        User loginUser = userDao.LoginUser(user);
        if(loginUser==null){
            //不正确-->失败页面
            req.getRequestDispatcher("/failServlet").forward(req,resp);
        }else{
            //正确-->成功页面
            //共享数据
            req.setAttribute("user",loginUser);
            req.getRequestDispatcher("/successServlet").forward(req,resp);

        }



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        this.doGet(req,resp);
    }
}
